define(['mui','ajax'], function() {
	var me = null;
	var base_page = null;
	var phoneType = null;
	var news = null,
		analysis = null;
	var ms = null;
	mui.init({
		swipeBack: true,
		beforeback: function() {
			//释放内存
			document.body.style.backgroundImage = null;

			base_page = null;
			loginButton = null, accountBox = null, passwordBox = null;
			regButton = null, forgetButton = null, transTime = null;
			phoneType = null;
			user = null;
			//					plus.webview.close(me);
			me = null;
			//返回true，继续页面关闭逻辑
			return true;
		}
	});

	var loginButton = document.getElementById('login');
	var accountBox = document.getElementById('account');
	var passwordBox = document.getElementById('password');
	//var regButton = document.getElementById('reg');
	var forgetButton = document.getElementById('forgetPassword');
	var transTime = getNowFormatDate();
	var user = {};
	mui.plusReady(function() {
		me = plus.webview.currentWebview();

		//				ms = me.opener();
		telephoneNumbefDefault(); //写上默认的电话号码
		ms = plus.webview.getWebviewById('index-menu.html');
		news = plus.webview.getWebviewById("report.html")
		analysis = plus.webview.getWebviewById("pages/analysis.html")
		base_page = plus.webview.getWebviewById("base_page.html");
		//timeindex = plus.webview.getWebviewById("pages/timeindex.html");
		plus.screen.lockOrientation("portrait-primary");
		
		forgetButton.addEventListener('tap', function() {
			openpage("findpwd.html");
		});
		//登陆,成功之后存储个人Token，字段名为memberdata ，user的json字符串
		loginButton.addEventListener('tap', function(event) {
			if(mui.os.android) {
				phoneType = 'Android';
			} else {
				phoneType = 'IOS';
			}
			var info = plus.push.getClientInfo();
			console.log('%o', info);
			var str;
			var base64;
			
			//require(['base64'],function(){
		    str = CryptoJS.enc.Utf8.parse(passwordBox.value);
		    base64 = CryptoJS.enc.Base64.stringify(str);
		    console.log(base64);
			//})
			console.log(base64);
			var data_form = {
				transtime: transTime,
				UserID: accountBox.value,
				Password: base64,
				ClientID: info.clientid == "" ? '123' : info.clientid,
				IOSToken: info.token,
				PhoneType: phoneType
			};
			console.log("data_form:", JSON.stringify(data_form));
			mui.post(ajax_loginURL, data_form, function(data_) {
				console.log('back:', data_)
				info = null, str = null, base64 = null;
				data = JSON.parse(data_);
				data_ = null;
				info = null, str = null, base64 = null;
				data_form = null;

				if(data.result == 1) {
					user.token = data.Token;
					user.phoneNumber = "" + accountBox.value;
					var memberdata = JSON.stringify(user);
					plus.storage.setItem('memberdata', memberdata);
					plus.storage.setItem('phoneNumber', accountBox.value);

					news.reload(false);

					memberdata = null;
					accountBox = null;
					plus.nativeUI.toast("登录成功");

					news.reload(false);

					mui.fire(ms, 'getUserInfoFromServer', {});
					if(base_page != null) {
						mui.fire(base_page, 'refresh_content', {});
					}
					mui.back();
					//								
				} else {
					mui.toast(data.message);
				}
			}, 'json');
		});

	});
	//密码显示功能
	mui('.mui-content .mui-switch')[0].addEventListener('toggle', function(event) {
		//event.detail.isActive 可直接获取当前状态
		if(event.detail.isActive) {
			//$("#password").attr("type","text");
			passwordBox.setAttribute("type", "text")
		} else {
			//$("#password").attr("type","password");
			passwordBox.setAttribute("type", "password")
		}
	});

	function telephoneNumbefDefault() {
		var telephone = plus.storage.getItem('phoneNumber');
		if(telephone) {
			accountBox.value = telephone;
		}
		telephone = null;
	}

	function toMain() {
		ms.evalJS("getuser(1)");
	}
})